package com.situ.blog.dao.impl;

import com.situ.blog.dao.IUserDao;
import com.situ.blog.pojo.User;
import com.situ.blog.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    @Override
    public List<User> selectByPage(int offset, Integer limit) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<User> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,password,role,nick_name,email,phone,image,status,deleted,gmt_create,gmt_modified from t_user limit ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, offset);
            statement.setInt(2, limit);
            System.out.println(statement);

            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                int role = resultSet.getInt("role");
                String nickName = resultSet.getString("nick_name");
                String email = resultSet.getString("email");
                String phone = resultSet.getString("phone");
                String image = resultSet.getString("image");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("deleted");
                // java.sql.Date
                Date gmtCreate = resultSet.getDate("gmt_create");
                Date gmtModified = resultSet.getDate("gmt_modified");

                User user = new User(id, name, password, role, nickName, email, phone, image, status, deleted, gmtCreate, gmtModified);
                list.add(user);
            }

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }

        return list;
    }

    @Override
    public int selectTotalCount() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from t_user ";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                count = resultSet.getInt(1);
            }

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }

        return count;
    }

    @Override
    public int add(User user) {
        Connection connection = null;
        PreparedStatement statement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into t_user(name,password,image,role) values(?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getName());
            statement.setString(2, user.getPassword());
            statement.setString(3, user.getImage());
            statement.setInt(4, user.getRole());
            System.out.println(statement);
            count = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }

        return count;
    }

    @Override
    public int updateStatus(int id, int status) {
        Connection connection = null;
        PreparedStatement statement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update t_user set status=? where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, status);
            statement.setInt(2, id);
            System.out.println(statement);
            count = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }

        return count;
    }

    @Override
    public User login(String name, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,password,role,nick_name,email,phone,image,status,deleted,gmt_create,gmt_modified from t_user where name=? and password=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, name);
            statement.setString(2, password);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                int id = resultSet.getInt("id");
                int role = resultSet.getInt("role");
                String nickName = resultSet.getString("nick_name");
                String email = resultSet.getString("email");
                String phone = resultSet.getString("phone");
                String image = resultSet.getString("image");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("deleted");
                // java.sql.Date
                Date gmtCreate = resultSet.getDate("gmt_create");
                Date gmtModified = resultSet.getDate("gmt_modified");

                user = new User(id, name, password, role, nickName, email, phone, image, status, deleted, gmtCreate, gmtModified);;
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }

        return user;
    }

}
